package com.xin.mmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xin.mmall.entity.*;
import com.xin.mmall.mapper.*;
import com.xin.mmall.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.mmall.vo.OrderDetailVO;
import com.xin.mmall.vo.OrderVO;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴玉鑫
 * @since 2021-05-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public boolean save(Orders orders, User user,String address,String remark) {

        //判断是新地址还是老地址
        if (orders.getUserAddress().equals("newAddress")){
            //存入
            System.out.println(address);
            System.out.println(user.getId());
            UserAddress userAddress = new UserAddress();
            userAddress.setAddress(address);
            userAddress.setRemark(remark);
            userAddress.setIsdefault(1);
            userAddress.setUserId(user.getId());
            //将老的地址isdefault改为0
            QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
            wrapper.eq("isdefault",1).eq("user_id",user.getId());
            UserAddress oldDefault = userAddressMapper.selectOne(wrapper);
            System.out.println(oldDefault);
            oldDefault.setIsdefault(0);
            userAddressMapper.updateById(oldDefault);
            //存值
            userAddressMapper.insert(userAddress);
            orders.setUserAddress(address);
        }
        orders.setUserId(user.getId());
        orders.setLoginName(user.getLoginName());
        String seriaNumber = null;
        try {
            StringBuffer result = new StringBuffer();
            for(int i=0;i<32;i++) {
                result.append(Integer.toHexString(new Random().nextInt(16)));
            }
            seriaNumber =  result.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        orders.setSerialnumber(seriaNumber);
        orderMapper.insert(orders);

//        save(orders);
        //存储ordersDetail
        QueryWrapper<Cart> cartQueryWrapper = new QueryWrapper<>();
        cartQueryWrapper.eq("user_id",user.getId());
        List<Cart> cartList = cartMapper.selectList(cartQueryWrapper);
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(cart,orderDetail);
            orderDetail.setId(null);
            orderDetail.setOrderId(orders.getId());
            orderDetailMapper.insert(orderDetail);
        }
        //清空购物车
        QueryWrapper<Cart> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id", user.getId());
        cartMapper.delete(wrapper1);
        return true;
    }


    @Override
    public List<OrderVO> ordersList(User user){
        //返回orderVOList
        List<OrderVO> orderVOList = new ArrayList<>();
        Map<String,Object> ordersHashMap = new HashMap<>();
        ordersHashMap.put("user_id",user.getId());
        //找到所有user_id的订单记录
        List<Orders> orders = orderMapper.selectByMap(ordersHashMap);
        //迭代所有订单
        for (Orders order : orders) {
            OrderVO orderVO = new OrderVO();
            //将order的值赋值给orderVO对应的属性。
            BeanUtils.copyProperties(order,orderVO);
            //对List<OrderDetailVO> orderDetailVOs赋值
            /*  private String name;//商品名称
                private String fileName;//商品图面
                private Float cost;//商品价格
                private Integer quantity;//商品数量
            * */
            //查询所有orderDetail记录
            ArrayList<OrderDetailVO> detailVOList = new ArrayList<>();
            HashMap<String, Object> detailMap = new HashMap<>();
            detailMap.put("order_id",order.getId());
            List<OrderDetail> orderDetails = orderDetailMapper.selectByMap(detailMap);
            for (OrderDetail orderDetail : orderDetails) {
                OrderDetailVO orderDetailVO = new OrderDetailVO();
                BeanUtils.copyProperties(orderDetail,orderDetailVO);
                Product product = productMapper.selectById(orderDetail.getProductId());
                BeanUtils.copyProperties(product,orderDetailVO);
                detailVOList.add(orderDetailVO);
            }
            orderVO.setOrderDetailVOs(detailVOList);
            orderVOList.add(orderVO);
        }

        return orderVOList;
    }

}
